package dataManagers;

import entity.Paths;
import java.util.*;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;



public enum PathDM{
    INSTANCE;

private PathDM(){}
public static void createPath(Paths path){
        PersistenceManager pm = PMF.get().getPersistenceManager();
        try{
            pm.makePersistent(new Paths(path.getPathName(),path.getPathID(),path.getImage(),path.getDownloads()));
        }catch(Exception e){

        }finally{
            pm.close();
        }

    }

    public static Paths[] retrieveAll() {
        PersistenceManager pm = PMF.get().getPersistenceManager();
        List<Paths> result = null;
        Query query=pm.newQuery(Paths.class);
        try {

            result=(List<Paths>)query.execute();
            result.size();
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            query.closeAll();
            pm.close();
        }
        return result.toArray(new Paths[result.size()]);
    }


    public static Paths retrieve(String pathID) {
        PersistenceManager pm = PMF.get().getPersistenceManager();
        Paths user = null;
        Query query = pm.newQuery(Paths.class);
        query.setFilter("pathID == pathIDParam");
        query.declareParameters("String pathIDParam");
        try {
           List<Paths> results = (List<Paths>) query.execute(pathID);
           if (results.iterator().hasNext()) {
                for (Paths e : results) {
                    user = e;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            query.closeAll();
            pm.close();
        }
        return user;
    }


    public static void remove(String pathID) {
        PersistenceManager pm = PMF.get().getPersistenceManager();
        Query query=pm.newQuery(Paths.class);
        query.setFilter("pathID == pathIDParam");
        query.declareParameters("String pathIDParam");

        try {
            query.deletePersistentAll(pathID);
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            query.closeAll();
            pm.close();
        }
    }

    public static void modify(Paths path) {
        PersistenceManager pm = PMF.get().getPersistenceManager();
        List<Paths> list=null;
        Query query=pm.newQuery(Paths.class);
        query.setFilter("pathID == pathIDParam");
        query.declareParameters("String pathIDParam");

        try {
            list = (List<Paths>)query.execute(path.getPathID());
            if (list.iterator().hasNext()){
                 for(Paths e: list){
                      e.setPathName(path.getPathName());
                      e.setPathID(path.getPathID());
                      e.setImage(path.getImage());
                      e.setDownloads(path.getDownloads());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            query.closeAll();
            pm.close();
        }
    }

}
